﻿using System;
using System.Xml;
using System.Text;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0800 //2011.03.14 //Begin
namespace InfoJetSoft.Service.Worker
{

    class XSNViewFormatSelectWorker
	{
        public static void formatSelect(View view)
        {
            StringBuilder optionFilterBuffer = new StringBuilder();
            optionFilterBuffer.Append(NamespaceUtil.toFullName(view.XslPrefix, "choose"));
            optionFilterBuffer.Append("/");
            optionFilterBuffer.Append(NamespaceUtil.toFullName(view.XslPrefix, "when"));
            optionFilterBuffer.Append("/");
            optionFilterBuffer.Append(NamespaceUtil.toFullName(view.XslPrefix, "for-each"));
            optionFilterBuffer.Append("/option");

            string optionFilter = optionFilterBuffer.ToString();

            string valueValueOfFilter = NamespaceUtil.toFullName(view.XslPrefix, "attribute") + "[@name='value']/" + NamespaceUtil.toFullName(view.XslPrefix, "value-of");
            string nameValueOfFilter = NamespaceUtil.toFullName(view.XslPrefix, "value-of");
            string variableFilter = "../../" + NamespaceUtil.toFullName(view.XslPrefix, "variable");
            string foreachFilter = "..";

            int index = 0;

            XmlNodeList selectElementList = view.Doc.SelectNodes("//select");
            foreach (XmlElement selectElement in selectElementList)
            {
                string toPicker = selectElement.GetAttribute("toPicker");
                if (toPicker.Equals("true"))
                {
                    XmlElement optionElement = (XmlElement)DOMUtil.selectANode(selectElement, optionFilter, view.NsResolver);
                    if (optionElement != null)
                    {
                        XmlElement valueValueOfElement = (XmlElement)DOMUtil.selectANode(optionElement, valueValueOfFilter, view.NsResolver);
                        XmlElement nameValueOfElement = (XmlElement)DOMUtil.selectANode(optionElement, nameValueOfFilter, view.NsResolver);
                        XmlElement variableElement = (XmlElement)DOMUtil.selectANode(optionElement, variableFilter, view.NsResolver);
                        XmlElement foreachElement = (XmlElement)DOMUtil.selectANode(optionElement, foreachFilter, view.NsResolver);
                        if (valueValueOfElement != null && nameValueOfElement != null && variableElement != null && foreachElement != null)
                        {
                            string valueFieldName = valueValueOfElement.GetAttribute("select");
                            string nameFieldName = nameValueOfElement.GetAttribute("select");
                            string variableFieldName = variableElement.GetAttribute("select");
                            string itemFieldName = foreachElement.GetAttribute("select");

                            XmlElement boxElement = view.Doc.CreateElement("span");
                            string style = selectElement.GetAttribute("style");
                            if (style.Length > 0)
                            {
                                boxElement.SetAttribute("style", style);
                            }
                            string bindingValue = DOMUtil.getControlBindingExpr(view, variableFieldName);
                            boxElement.SetAttribute("class", "xdTextBox");
                            boxElement.SetAttribute("dropdownListId", index.ToString());

                            XmlElement spanElement = view.Doc.CreateElement("span");
                            boxElement.AppendChild(spanElement);
                            spanElement.SetAttribute("style", "width:100%");

                            XmlElement buttonElement = view.Doc.CreateElement("button");
                            boxElement.AppendChild(buttonElement);
                            buttonElement.SetAttribute("id", "InfoJet_PopupDropdownButton");
                            buttonElement.SetAttribute("style", "width:17px;height:17px;margin-left:-17px;border:none;");
                            buttonElement.SetAttribute("onclick", "InfoJet_OnDropdownListPickerClick(event, this);");
                            XmlElement imageElement = view.Doc.CreateElement("img");
                            imageElement.SetAttribute("style", "width:16px;height:16px;");
                            imageElement.SetAttribute("src", view.Manifest.ImageUri.Dropdown);
                            buttonElement.AppendChild(imageElement);

                            XmlElement varElement = view.Doc.CreateElement(view.XslPrefix, "variable", JetConstant.URI_XSL);
                            varElement.SetAttribute("name", "val");
                            varElement.SetAttribute("select", variableFieldName);
                            spanElement.AppendChild(varElement);
                            
                            XmlElement valueOfElement = view.Doc.CreateElement(view.XslPrefix, "value-of", JetConstant.URI_XSL);
                            valueOfElement.SetAttribute("select", itemFieldName + "/" + nameFieldName + "[../" + valueFieldName + " = $val]");
                            spanElement.AppendChild(valueOfElement);

                            selectElement.ParentNode.ReplaceChild(boxElement, selectElement);
                            while (selectElement.ChildNodes.Count > 0)
                            {
                                selectElement.RemoveChild(selectElement.FirstChild);
                            }
                            selectElement.SetAttribute("style", "display:none");
                            boxElement.AppendChild(selectElement);

                            DropdownListInfo info = new DropdownListInfo();
                            info.Id = index.ToString();
                            info.ItemsPath = itemFieldName;
                            info.NamePath = nameFieldName;
                            info.ValuePath = valueFieldName;

                            if (view.Manifest.DrondownListInfos == null)
                            {
                                view.Manifest.DrondownListInfos = new System.Collections.ArrayList();
                            }
                            view.Manifest.DrondownListInfos.Add(info);

                            index++;
                        }
                    }
                }
            }
        }
 
	}
}
//CID 0800 //2011.03.14 //End